import axios from 'axios'
// import store from '@/store'
// import { getUserInfo } from '@/utils/storage'
// import { getUserToken } from '@/utils/storage'
import { Message } from 'element-ui'
import { getToken } from '@/utils/storage'
const request = axios.create({
  baseURL: '/api',
  timeout: 5000,
  headers: {
    'Content-Type': 'application/json'
    // Authorization: token
  }
})
// 添加请求拦截器
request.interceptors.request.use(
  config => {
    const token = JSON.parse(getToken())
    if (token) {
      config.headers.Authorization = token
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 添加响应拦截器
request.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  // const { msg, success } = response.data
  // console.log(response)
  if (response.status === 200) {
    // Message.success(response.data.msg)
    return response.data
  } else {
    Message(response.data.msg)
    return Promise.reject(new Error(response.data.msg))
  }
}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error)
})

export default request
